﻿using UnityEngine;
using System.Collections;

public class ByteArray
{

    private const int INTLENGTH = 4;

    private const int BYTESINITLENGTH = 1024;
    /// <summary>
    /// 游标
    /// </summary>
    public int position = 0;

    /// <summary>
    /// bytes中存储的数据长度
    /// </summary>
    public int length = 0;


    public byte[] bytes;

    public ByteArray()
    {
        bytes = new byte[BYTESINITLENGTH];
    }

    public ByteArray(int length)
    {
        if (length == 0)
        {
            bytes = new byte[BYTESINITLENGTH];
        }
        else
        {
            bytes = new byte[length];
        }
    }

    /// <summary>
    /// 读取一个字节，position游标后移一位
    /// </summary>
    public byte ReadByte()
    {
        return bytes[position];
        position++;
    }

    /// <summary>
    /// 读取字节数组并写入value，传入读取长度length，position游标后移length位，返回读取到的长度
    /// </summary>
    public int ReadBytes(byte[] value, int len)
    {
        if (value != null)
        {
            int i = 0;
            int maxRead = length - position;
            if (value.Length < len)
            {
                while (i < value.Length)
                {
                    value[i] = bytes[position];
                    i++;
                    position++;
                    if (position == length)
                        break;
                }
                return i;
            }
            else
            {
                while (i < len)
                {
                    value[i] = bytes[position];
                    i++;
                    position++;
                    if (position == length)
                        break;
                }
                return i;
            }
        }
        else
        {
            return -1;
        }
    }

    /// <summary>
    /// 读取字节数组并写入value，传入读取长度len，position游标后移len位，返回读取到的长度
    /// </summary>
    public int ReadBytes(ByteArray value, int len)
    {
        if (value != null)
        {
            int i = 0;
            while (i < len)
            {
                value.WriteByte(bytes[position]);
                i++;
                position++;
                if (position == length)
                    break;
            }
            return i;
        }
        else
        {
            return -1;
        }
    }

    /// <summary>
    /// 读取一个四个字节长度的int
    /// </summary>
    public int ReadInt()
    {
        byte[] tempBytes = new byte[INTLENGTH];
        for (int i = 0; i < INTLENGTH; i++)
        {
            tempBytes[i] = bytes[position];
            position++;
        }
        int value = System.BitConverter.ToInt32(tempBytes, 0);
        return value;
    }

    /// <summary>
    /// 读取指定字节长度的字符串
    /// </summary>
    public string ReadStr(int len)
    {
        if (len > bytes.Length - position)
        {
            len = bytes.Length - position;
        }
        byte[] tempBytes = new byte[len];
        for (int i = 0; i < len; i++)
        {
            tempBytes[i] = bytes[position];
            position++;
        }
        //string value = System.Text.Encoding.Default.GetString(tempBytes);
        string value = System.Text.Encoding.UTF8.GetString(tempBytes);
        return value;
    }

    /// <summary>
    /// 写入一个字节，返回是否成功
    /// </summary>
    public bool WriteByte(byte value)
    {
        if (value != null)
        {
            if (position >= bytes.Length)
            {
                byte[] bytesNew = new byte[bytes.Length * 2];
                bytes.CopyTo(bytesNew, 0);
                bytes = bytesNew;
            }
            bytes[position] = value;
            position++;
            length++;
            return true;
        }
        else
        {
            return false;
        }
    }

    /// <summary>
    /// 写入字节数组，传入要写入的字节数组和写入长度，返回写入长度
    /// </summary>
    public int WriteBytes(byte[] value, int len)
    {
        if (value != null)
        {
            while (position + value.Length >= bytes.Length)
            {
                byte[] bytesNew = new byte[bytes.Length * 2];
                bytes.CopyTo(bytesNew, 0);
                bytes = bytesNew;
            }
            if (value.Length < len)
            {
                return -1;
            }
            else
            {
                for (int i = 0; i < len; i++)
                {
                    bytes[position] = value[i];
                    position++;
                    length++;
                }
                return len;
            }
        }
        else
        {
            return -1;
        }
    }

    /// <summary>
    /// 根据传入的ByteArray对象写入，从value的index处开始读，到len结束	
    /// </summary>
    public void WriteBytes(ByteArray value, int index, int len)
    {
        int i = index;
        while (i < index + len)
        {
            bytes[position] = value.bytes[i];
            i++;
            position++;
            length++;
        }
    }

    /// <summary>
    /// 写入一个int，返回写入的字节长度（成功则为4，失败返回-1）
    /// </summary>
    public int WriteInt(int value)
    {
        if (value != null)
        {
            while (position + INTLENGTH >= bytes.Length)
            {
                byte[] bytesNew = new byte[bytes.Length * 2];
                bytes.CopyTo(bytesNew, 0);
                bytes = bytesNew;
            }
            byte[] bytesTemp = new byte[4];
            bytesTemp = System.BitConverter.GetBytes(value);
            for (int i = 0; i < INTLENGTH; i++)
            {
                bytes[position] = bytesTemp[i];
                position++;
                length++;
            }
            return INTLENGTH;
        }
        else
        {
            return -1;
        }
    }

    /// <summary>
    /// 写入一个字符串
    /// </summary>
    public int WriteStr(string value)
    {
        if (value != null)
        {
            byte[] bytesTemp = System.Text.Encoding.Default.GetBytes(value);
            while (position + bytesTemp.Length >= bytes.Length)
            {
                byte[] bytesNew = new byte[bytes.Length * 2];
                bytes.CopyTo(bytesNew, 0);
                bytes = bytesNew;
            }
            foreach (byte b in bytesTemp)
            {
                bytes[position] = b;
                position++;
                length++;
            }
            return bytesTemp.Length;
        }
        else
        {
            return -1;
        }
    }

    /// <summary>
    /// 将游标和长度都置为0
    /// </summary>
    public void Clear()
    {
        position = 0;
        length = 0;
    }
}